package com.stars.pao.message.controller;

import com.stars.pao.message.model.MessageInfo;
import com.stars.pao.message.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/message")
@RestController
public class MessageController {

    private List<MessageInfo> messageInfos = new ArrayList<>();

    @Autowired
    private MessageService messageService;

    @RequestMapping("/publish")
    public boolean publishMessage(MessageInfo messageInfo) {

        if(!StringUtils.hasLength(messageInfo.getFrom())
                ||!StringUtils.hasLength(messageInfo.getMessage())
                ||!StringUtils.hasLength(messageInfo.getTo())){
            return false;
        }
//        暂存在内存中
//        messageInfos.add(messageInfo);
//        放在数据库中
        messageService.insertMessage(messageInfo);
        return true;
    }

    @RequestMapping("/getList")
    public List<MessageInfo> receiveMessage() {

        return messageService.queryMessageList();
    }
}

